//
// Created by taojiaheng on 2020/6/5.
//

#include "Text.h"

Text::Text():Protocol() {

}

Text::~Text() {

}

int Text::input(BUFF *buff, Connection *client) {
    for(int i = 0; i < buff->buff_len; i++) {
        if(buff->buff[i] == '\n'){
            return i + 1;
        }
    }
    return 0;
}

BUFF *Text::decode(BUFF *_buff, Connection *client) {
    BUFF * temp_buff = new BUFF;
    temp_buff->buff = new char[_buff->buff_len - 1];
    memcpy((void *)temp_buff->buff, (void *)_buff->buff, _buff->buff_len - 1);
    temp_buff->buff_len = _buff->buff_len - 1;
    return temp_buff;
}

BUFF *Text::encode(BUFF *_buff, Connection *client) {
    BUFF * temp_buff = new BUFF;
    temp_buff->buff = new char[_buff->buff_len + 1];
    temp_buff->buff_len = _buff->buff_len + 1;
    memcpy((void*) temp_buff->buff, (void *)_buff->buff, _buff->buff_len);
    temp_buff->buff[_buff->buff_len] = '\n';
    return temp_buff;
}

int Text::handshake(BUFF *_buff, Connection *client) {
    return 0;
}


